import time

from .config_data.case_data import *
import pytest
from tool_class.page_handle import DataTreatingPageHandle


def basic_configuration_module(handle,el1,el2):
    #进入车辆基础配置页面
    handle.goto_local(url_path="/ins/#/settings/baseSettings")
    car_type = handle.get_list_data(el1)
    car_class = handle.get_list_data(el2)
    energy_type = {}
    for itme1,itme2 in zip(car_type, car_class):
        if "标签" in itme1: #过滤掉标签
            continue
        if itme1 in energy_type:
            energy_type[itme1].append(itme2)
            continue
        energy_type[itme1] = [itme2]
    return energy_type

def basic_configuration_module02(handle,el1,el2,el3):
    #进入标签基础配置页面
    handle.goto_local(el3,url_path="/ins/#/settings/baseSettings")
    car_type = handle.get_list_data(el1)
    car_class = handle.get_list_data(el2)
    energy_type = {}
    for itme1,itme2 in zip(car_type, car_class):
        if itme1 in energy_type:
            energy_type[itme1].append(itme2)
            continue
        energy_type[itme1] = [itme2]
    return energy_type

@pytest.fixture(scope='function')
def basic_configuration_function(session_page):
    session_page = session_page
    handle = DataTreatingPageHandle(session_page)
    yield session_page, handle
    #返回基础配置页面
    handle.goto_local(url_path="/ins/#/settings/baseSettings")

def test_energy_type_01(basic_configuration_function):
    session_page, handle = basic_configuration_function
    energy_type = basic_configuration_module(handle,"baseSettings-2001-t1","baseSettings-2001-t3")
    handle.goto_local("founding-tabs-1003",url_path="/ins/#/dataCenter/founding")
    result_list = []
    for car in energy_type:
        handle.pull_cascade("founding-carseries-10003", [f"{car}>click"])
        for itme in energy_type[car]:
            result = session_page.locator(f'[role="menuitem"][title="{itme}"]').nth(-1).is_visible()
            result_list.append(result)
    assert all(itme for itme in result_list)

def test_energy_type_02(basic_configuration_function):
    session_page, handle = basic_configuration_function
    energy_type = basic_configuration_module(handle,"baseSettings-2001-t1","baseSettings-2001-t3")
    handle.goto_local("founding-tabs-1003>founding-table-10001",url_path="/ins/#/dataCenter/founding") #去到新增车系
    result_list = []
    handle.pull_checked("founding-carseries-series-40009","founding-carseries-series-40009-0")
    print("energy_type---",energy_type)
    for car in energy_type:
        handle.pull_text_checked("founding-carseries-series-40011","founding-carseries-series-40011-",car)
        for itme in energy_type[car]:
            handle.pull_text_checked("founding-carseries-series-40012","founding-carseries-series-40012-",itme)
            print("itme---",itme)
            result = handle.get_values({itme: "founding-carseries-series-40012"})
            result_list.append(result[itme] == itme)
    assert all(itme for itme in result_list)

def test_car_type_01(basic_configuration_function):
    session_page, handle = basic_configuration_function
    car_type = basic_configuration_module(handle,"findVehicleInfo-baseSettings-2001-t0","findVehicleInfo-baseSettings-2001-t5")
    for itme in car_type:
        if "、" in car_type[itme][0]:
            car_type[itme] = car_type[itme][0].split("、")
    print("energy_type---",car_type)
    handle.goto_local("founding-tabs-1003",url_path="/ins/#/dataCenter/founding")
    result_list = []
    for car in car_type:
        handle.pull_cascade("founding-carseries-10002", [f"{car}>click"])
        for itme in car_type[car]:
            result = session_page.locator(f'[role="menuitem"][title="{itme}"]').nth(-1).is_visible()
            result_list.append(result)
    assert all(itme for itme in result_list)

def test_car_type_02(basic_configuration_function):
    session_page, handle = basic_configuration_function
    car_type = basic_configuration_module(handle,"findVehicleInfo-baseSettings-2001-t0","findVehicleInfo-baseSettings-2001-t5")
    for itme in car_type:
        if "、" in car_type[itme][0]:
            car_type[itme] = car_type[itme][0].split("、")
    handle.goto_local("founding-tabs-1003>founding-table-10001",url_path="/ins/#/dataCenter/founding") #去到新增车系
    # handle.pull_checked("founding-carseries-series-40007","founding-carseries-series-40007-0")
    print("energy_type---",car_type)
    result_obj = {}
    for car in car_type:
        handle.pull_text_checked("founding-carseries-series-40007","founding-carseries-series-40007-",car)
        print("itme---",car)
        # 如果键不存在，初始化一个空列表
        if car not in result_obj:
            result_obj[car] = []
        handle.element_click("founding-carseries-series-40008")
        result_obj[car]=handle.get_ul_value("founding-carseries-series-40008")
    print("result_obj---",result_obj)
    print("car_type---",car_type)
    assert result_obj == car_type

def test_tag_type_01(basic_configuration_function):
    session_page, handle = basic_configuration_function
    tag_type = basic_configuration_module02(handle,"findLabelTypeInfo-baseSettings-2001-t1"
                                            ,"findLabelTypeInfo-baseSettings-2001-t3","baseSettings-1004")
    for itme in tag_type:
        if "、" in tag_type[itme][0]:
            tag_type[itme] = tag_type[itme][0].split("、")
    print("energy_type---",tag_type)
    handle.goto_local("application-20002",url_path="/ins/#/tagManagement/application")
    result_list = []
    for tag in tag_type:
        handle.pull_text_checked("application-form-10002", "application-form-10002-",tag)
        for itme in tag_type[tag]:
            itme = f"关联{itme}" if "能源类型" != itme else "关联能源分类"
            itme = "严重性等级" if "关联严重性等级" == itme else itme
            print(itme)
            result = session_page.locator(f'label:has-text("{itme}")').is_visible()
            result_list.append(result)
    assert all(itme for itme in result_list)

def test_severity_grade_01(basic_configuration_function):
    session_page, handle = basic_configuration_function
    severity_grade = basic_configuration_module02(handle,"findSeriousnessInfo-baseSettings-2001-t1-"
                                            ,"findSeriousnessInfo-baseSettings-2001-t1-","baseSettings-1005")
    print("energy_type---",severity_grade)
    handle.goto_local("application-20002",url_path="/ins/#/tagManagement/application")
    handle.pull_text_checked("application-form-10002", "application-form-10002-", "质量标签")
    result_list = []
    for itme in severity_grade:
        result = session_page.locator(f'[data-testid="application-formform-item-10006"]:has-text("{itme}")').is_visible()
        result_list.append(result)
    assert all(itme for itme in result_list)

def test_user_journey_01(basic_configuration_function):
    session_page, handle = basic_configuration_function
    severity_grade = basic_configuration_module02(handle,"findUserJourneyInfo-baseSettings-2001-t1-"
                                            ,"findUserJourneyInfo-baseSettings-2001-t1-","baseSettings-1006")
    print("energy_type---",severity_grade)
    handle.goto_local("application-20002",url_path="/ins/#/tagManagement/application")
    result_list = []
    for itme in severity_grade:
        result = session_page.locator(f'[data-testid="application-formform-item-10009"]:has-text("{itme}")').is_visible()
        result_list.append(result)
    assert all(itme for itme in result_list)